package com.yc.navy.goods.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.yc.navy.goods.bean.ClassInfo;
import com.yc.navy.goods.bean.TypeInfo;
import com.yc.navy.goods.dto.ConstantInfoDTO;
import com.yc.navy.goods.dto.TypeDTO;
import com.yc.navy.goods.dto.TypeInfoDTO;
import com.yc.navy.goods.enums.ResultEnum;
import com.yc.navy.goods.service.ITypeInfoService;
import com.yc.navy.goods.vo.ResultVO;
import com.yc.navy.goods.vo.StreamVO;

@RestController
@RequestMapping("/type")
public class TypeInfoController {
	@Autowired
	private ITypeInfoService typeInfoService;
	
	@RequestMapping("/list")
	public StreamVO list(@RequestParam Map<String,Object> map){
		map.forEach((key,val)->{
			System.out.println("["+key+":" + val + "]");
		});
		
		return typeInfoService.findByPageForBack(map);
	}
	
	@RequestMapping("/add")
	public ResultVO add(TypeInfo tf,MultipartFile photo) {
		String savepath = "";
		File file = null;
		
		if(photo != null && photo.getSize() > 0) {
			try {
				savepath = ConstantInfoDTO.UPLOADPATH + "/" + System.currentTimeMillis() + "_" +  photo.getOriginalFilename();
				file = new File(ConstantInfoDTO.basepath,savepath);
				photo.transferTo(file);
				tf.setPic(savepath);
			} catch (IllegalStateException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		int result = typeInfoService.add(tf);
		if(result == -1) {
			return new ResultVO(ResultEnum.ERROR);
		}
		
		if(result > 0) {
			return new ResultVO(ResultEnum.SUCCESS);
		}
		return new ResultVO(ResultEnum.ERROR);
		
	}
	
	@RequestMapping("/update")
	public ResultVO update(TypeInfo tf) {
		int result = typeInfoService.updateStatue(tf);
		if(result == -1) {
			return new ResultVO(ResultEnum.INCOMPLETEINFORMATION);
		}
		if(result <= 0) {
			return new ResultVO(ResultEnum.ERROR);
		}
		return new ResultVO(ResultEnum.SUCCESS,"hhh");
	}
	/**
	 * 删除类别 !
	 * @param tid
	 * @return
	 */
	@RequestMapping("/delete")
	public ResultVO delete(int tid) {
		int result = typeInfoService.delete(tid);
		if(result <= 0) {
			return new ResultVO(ResultEnum.ERROR);
		}
		
		return new ResultVO(ResultEnum.SUCCESS);
	}
	
	@RequestMapping("/finds")
	public StreamVO finds() {
		List<TypeInfoDTO> list = typeInfoService.finds();
		if(list == null || list.isEmpty()) {
			return new StreamVO(ResultEnum.ERROR);
		}
		return new StreamVO(ResultEnum.SUCCESS,list);
	}
	
	@RequestMapping("/findclass")
	public StreamVO findclass() {
		List<ClassInfo> list = typeInfoService.findclass();
		if(list == null || list.isEmpty()) {
			return new StreamVO(ResultEnum.ERROR);
		}
		return new StreamVO(ResultEnum.SUCCESS,list);
	}
	
	@RequestMapping("/findsitems")
	public ResultVO findsitems() {
		List<TypeDTO> list = typeInfoService.findsitems();
		if(list == null || list.isEmpty()) {
			return new ResultVO(ResultEnum.ERROR);
		}
		return new ResultVO(ResultEnum.SUCCESS,list);
	}
}
